package com.culture.config.wrapper;

import com.culture.config.common.util.BeanUtil;
import com.culture.config.common.util.FunctionUtil;
import com.culture.config.constant.ScheduleStatus;
import com.culture.config.dao.entity.JobInfo;
import com.culture.config.dao.entity.JobQuery;
import com.culture.config.model.po.JobAddPO;
import com.culture.config.model.po.JobPagePO;
import com.culture.config.model.po.JobStatusUpdatePO;
import com.culture.config.model.po.JobUpdatePO;
import com.culture.config.util.AuthenticationUtil;

import lombok.experimental.UtilityClass;

@UtilityClass
public class JobWrapper {
    public static final String LOCK_TIMESPAN = "00:01:30";

    public JobQuery wrapScheduleJobQuery(){
        return new JobQuery();
    }

    public JobInfo wrapJobLockable(JobInfo jobInfo,String key){
        JobInfo lockJob = new JobInfo(jobInfo.getId()); 
        lockJob.setLockKey(key);
        lockJob.setScheduleStatus(ScheduleStatus.START.id());
        lockJob.setLockTimespan(LOCK_TIMESPAN);
        AuthenticationUtil.setUpdateFieldInfo(lockJob);
        return lockJob;
    }   

    public JobQuery wrapJobQuery(JobPagePO jobPagePO){
        JobQuery jobQuery = new JobQuery();
        FunctionUtil.nonBlank(jobPagePO.getName(), jobQuery::setName);
        FunctionUtil.nonBlank(jobPagePO.getType(), jobQuery::setType);
        jobQuery.setScheduleStatusList(jobPagePO.getScheduleStatusList());
        return jobQuery;
    }

    public JobInfo wrapJobAdd(JobAddPO jobAddPO){
        JobInfo jobInfo = BeanUtil.copyProperties(jobAddPO, JobInfo.class);     
        jobInfo.setDeleted(0);
        AuthenticationUtil.setInsertFieldInfo(jobInfo);
        return jobInfo;
    }

    public JobInfo wrapJobUpdate(JobInfo oldInfo, JobUpdatePO jobUpdatePO){
        JobInfo jobUpdateInfo = BeanUtil.copyProperties(jobUpdatePO, JobInfo.class);
        jobUpdateInfo.setId(oldInfo.getId());       
        AuthenticationUtil.setUpdateFieldInfo(jobUpdateInfo);
        return jobUpdateInfo;
    }

    public JobInfo wrapJobStatusUpdate(JobInfo oldInfo, JobStatusUpdatePO jobStatusUpdatePO){
        JobInfo jobUpdateInfo = new JobInfo(oldInfo.getId());       
        jobUpdateInfo.setScheduleStatus(jobStatusUpdatePO.getScheduleStatus());
        AuthenticationUtil.setUpdateFieldInfo(jobUpdateInfo);
        return jobUpdateInfo;
    }

    public JobInfo wrapJobDelete(JobInfo oldInfo){
        JobInfo jobUpdateInfo = new JobInfo(oldInfo.getId());
        jobUpdateInfo.setDeleted(1);
        AuthenticationUtil.setUpdateFieldInfo(jobUpdateInfo);
        return jobUpdateInfo;
    }
}
